<?php
//Raw protection
if (!$GET_service || $GET_service != "register") {
  die("<script>window.location='../?'</script>");
}
 
  //Iniciando validacoes
  //$data = $POST_data;
    $result = $db->select_one("SELECT usu_username FROM usuario WHERE usu_username LIKE '".$_POST["username"]."' ");
    if ($result != "") {
      die("ALREADYEXISTS");
    }

    $username = $_POST["username"];
    $password = $_POST["password"];
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $email = $_POST["email"];
    if (!$username || !$password || !$firstname || !$lastname || !$email) {
      die("EMPTYFIELDS");
    }
    
    $db->begin();
    //Valida username novamente
    $result = $db->select_one("SELECT usu_username FROM usuario WHERE usu_username LIKE '".$_POST["username"]."' ");
    if ($result != "") {
      die("ERROR");
    }
    //if ($db->select_one("SELECT usu_email FROM usuario WHERE usu_email = '$email'")) {
    //  die("ERROR");
    //}
    
    $result1 = $db->insert_sql("INSERT INTO `usuario` (`usu_username` ,`usu_password` ,`usu_email`, `usu_firstname`, `usu_lastname`)VALUES ('".$username."', '".$password."', '".$email."', '".$firstname."', '".$lastname."');");
    if ($result1) {
      $db->commit();
      die(json_encode(Array("status" => "OK")));
    } else {
      $db->rollback();
      die(json_encode(Array("status" => "ERROR")));
    }
?>
